package trees;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MinSubArrayLen {
    public int minSubArrayLen(int target, int[] nums) {
        List<Integer> resultLength = new ArrayList<>();
        int slowIndex = 0;
        int num = 0;
        for (int fastIndex = 0; fastIndex < nums.length; fastIndex++) {
            num += nums[fastIndex];
            if (num >= target) {
                resultLength.add(fastIndex - slowIndex + 1);
                slowIndex = slowIndex + 1;
                fastIndex = slowIndex - 1;
                num = 0;
            }
        }

        if (resultLength.size() == 0) {
            return 0;
        }

        return Collections.min(resultLength);
    }

    public static void main(String[] args) {
        int target = 11;
        int[] nums = new int[]{1, 1, 1, 1, 1, 1, 1};
        MinSubArrayLen minSubArrayLen = new MinSubArrayLen();
        System.out.println(minSubArrayLen.minSubArrayLen(target, nums));
    }
}
